package com.ktg.mes.rec.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.core.domain.entity.SysDictData;
import com.ktg.common.utils.SecurityUtils;
import com.ktg.mes.rec.service.IRecDispersedRecordService;
import com.ktg.mes.rec.service.IRecGrindRecordService;
import com.ktg.mes.rec.service.IRecPowderedRecordService;
import com.ktg.system.service.ISysDictDataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktg.mes.rec.mapper.RecTestingRecordMapper;
import com.ktg.mes.rec.domain.RecTestingRecord;
import com.ktg.mes.rec.service.IRecTestingRecordService;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;


/**
 * 检测记录Service业务层处理
 *
 * @author byteco
 * @date 2022-10-31
 */
@Service
public class RecTestingRecordServiceImpl extends ServiceImpl<RecTestingRecordMapper, RecTestingRecord> implements IRecTestingRecordService
{
    @Autowired
    private RecTestingRecordMapper recTestingRecordMapper;

    @Autowired
    private IRecDispersedRecordService recDispersedRecordService; //分散
    @Autowired
    private IRecGrindRecordService recGrindRecordService; //研磨
    @Autowired
    private IRecPowderedRecordService recPowderedRecordService; //加粉
    @Autowired
    private ISysDictDataService dictDataService;



    /**
     * 查询检测记录
     *
     * @param entity 检测记录
     * @param page 分页对象
     * @return 检测记录
     */
    @Override
    public IPage<RecTestingRecord> queryPageList(RecTestingRecord entity, Page<RecTestingRecord> page) {
        LambdaQueryWrapper<RecTestingRecord> lqw = buildQueryWrapper(entity);
        lqw.orderByDesc(RecTestingRecord::getCreateTime);
        IPage<RecTestingRecord> pageList = this.page(page, lqw);
        return pageList;
    }

    /**
     * 查询检测记录
     *
     * @param entity 检测记录
     * @return 检测记录
     */
    @Override
    public List<RecTestingRecord> queryList(RecTestingRecord entity) {
        LambdaQueryWrapper<RecTestingRecord> lqw = buildQueryWrapper(entity);
        lqw.orderByDesc(RecTestingRecord::getCreateTime);
        return this.list(lqw);
    }

    public RecTestingRecord getInfo(Long id)
    {
        QueryWrapper<RecTestingRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(RecTestingRecord::getTestingId, id);
        return this.getOne(queryWrapper);
    }


    /**
     * 新增检测记录
     *
     * @param entity 检测记录
     * @return 结果
     */
    @Override
    public void insert(RecTestingRecord entity)
    {
        entity.setCreateTime(new Date());
        entity.setCreateBy(SecurityUtils.getUsername());
        this.save(entity);
    }

    /**
     * 修改检测记录
     *
     * @param entity 检测记录
     * @return 结果
     */
    @Override
    public boolean update(RecTestingRecord entity)
    {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(new Date());
        return this.updateById(entity);
    }

    /**
     * 批量删除检测记录
     *
     * @param testingIds 需要删除的检测记录主键
     * @return 结果
     */
    @Override
    public boolean delete(Long[] testingIds)
    {

        return this.removeByIds(Arrays.asList(testingIds));
    }

    /**
     * 删除检测记录信息
     *
     * @param testingId 检测记录主键
     * @return 结果
     */
    @Override
    public boolean delete(Long testingId)
    {
        return this.removeById(testingId);
    }

    /**
     * 选择数据记录具体桶
     */
    @Override
    public List selectRecord(String testingType) {
        // SysDictData sysDictData = new SysDictData();
        // sysDictData.setDictType("SysDictData");
        // List<SysDictData> list = dictDataService.selectDictDataList(sysDictData);
        // SysDictData dictData = list.stream().filter(sysDictData1 -> sysDictData1.getDictValue().equals(testingType)).findAny().get();
        List listData = null;
            switch (testingType){
                case "POWDERED_TYPE":
                    listData = recPowderedRecordService.list();
                    break;
                case "GRIND_TYPE":
                    listData = recGrindRecordService.list();
                    break;
                case "DISPERSED_TYPE":
                    listData = recDispersedRecordService.list();
                    break;
                default:
                    listData = new ArrayList();
            }
        return listData;
    }

    private LambdaQueryWrapper<RecTestingRecord> buildQueryWrapper(RecTestingRecord entity) {
        LambdaQueryWrapper<RecTestingRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(entity.getToolTypeCode()), RecTestingRecord::getToolTypeCode, entity.getToolTypeCode());
        lqw.like(StringUtils.isNotBlank(entity.getToolTypeName()), RecTestingRecord::getToolTypeName, entity.getToolTypeName());
        lqw.eq(StringUtils.isNotBlank(entity.getSpecification()), RecTestingRecord::getSpecification, entity.getSpecification());
        lqw.like(StringUtils.isNotBlank(entity.getTaskName()), RecTestingRecord::getTaskName, entity.getTaskName());
        lqw.eq(entity.getStartTime() != null, RecTestingRecord::getStartTime, entity.getStartTime());
        lqw.eq(StringUtils.isNotBlank(entity.getViscosity()), RecTestingRecord::getViscosity, entity.getViscosity());
        lqw.eq(StringUtils.isNotBlank(entity.getFineness()), RecTestingRecord::getFineness, entity.getFineness());
        lqw.eq(StringUtils.isNotBlank(entity.getPitting()), RecTestingRecord::getPitting, entity.getPitting());
        return lqw;
    }
}
